package com.telnetnz.ticketdesk.action.acp.user;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.rockbb.common.simpleframe.config.SimpleConfig;
import com.rockbb.common.sso.session.SessionConstants;
import com.rockbb.common.utilities.text.ComponentBuilder;
import com.rockbb.common.utilities.text.StringUtils;
import com.telnetnz.ticketdesk.action.base.BasicSessionPageAcpAction;
import com.telnetnz.ticketdesk.action.common.CommonUtils;
import com.telnetnz.ticketdesk.bean.ProjectToUser;
import com.telnetnz.ticketdesk.bean.User;
import com.telnetnz.ticketdesk.service.ProjectService;
import com.telnetnz.ticketdesk.service.ProjectToUserService;
import com.telnetnz.ticketdesk.service.UserService;

public class AcpUserListAction extends BasicSessionPageAcpAction
{
	private static Logger logger = Logger.getLogger(AcpUserListAction.class);

	public String doView()
	{
		RequestBean bean = new DefaultRequestBean();
		long id = bean.getLong("id", 0L);
		User user = UserService.getUser(id);
		if (user == null)
		{
			return this.message_box(
					langRes.get("global.error"),
					langRes.get("user.user-not-exist", new String[]{bean.getRedirect()}),
					bean.getRedirect(),
					2);
		}

		this.put_page_key("user", user);
		this.put_page_key("languages", getLanguages());
		this.put_page_key("add_project_action", baseLink("acp/projectmemberadd"));

		List<ProjectToUser> projects = user.getProjectUsers();
		List<Integer> projectIds = new ArrayList<Integer>();
		for (ProjectToUser project : projects)
			projectIds.add(project.getProjectId());
		this.put_page_key("project_options", CommonUtils.buildProjectDropdown(
				ProjectService.getByPageAndCondition(0, 100, ProjectService.ORDERBY_NAME, 1, projectIds), 
				0, 
				false));
		this.put_page_key("role_options", CommonUtils.buildSingleDropdown(langRes, "project.role.", ProjectToUserService.ROLES, 0, false));
		return SUCCESS;
	}

	public String doList()
	{
		ListUserBean bean = new ListUserBean();
		String page_nav = ComponentBuilder.generate_pagination(
				baseLink()+bean.getParams(), 
				"start", 
				bean.getSize(), 
				bean.getStep(), 
				bean.getStart(), 
				false, 
				"");

		List<User> users = UserService.getByPageAndCondition(
				bean.getStart(), 
				bean.getStep(), 
				bean.getOrderby(), 
				bean.getOrder(), 
				SessionConstants.USER_ID_ANONYMOUS, 
				bean.getName(), 
				bean.getEmail());

			this.put_page_key("page_nav", page_nav);
			this.put_page_key("sys_users", users);
			this.put_page_key("bean", bean);
			this.put_page_key("orderby_options", CommonUtils.buildOrderByDropdown(langRes, "user.orderby.", UserService.ORDER_BY, bean.getOrderby()));
			this.put_page_key("order_options", CommonUtils.buildOrderDropdown(langRes, "global.order.", UserService.ORDER, bean.getOrder()));
			this.put_page_key("status_options", CommonUtils.buildSingleDropdown(langRes, "user.status.", UserService.STATUSES, bean.getStatus(), true));
			this.put_page_key("post_action", baseLink());

			return SUCCESS;
	}

	public class ListUserBean extends RequestBean
	{
		private int start;
		private int step;
		private int orderby;
		private int order;
		private String name;
		private String email;
		private int status;
		private long size;
		private String params;

		public int getStart() {
			return start;
		}

		public int getStep() {
			return step;
		}

		public int getOrderby() {
			return orderby;
		}

		public int getOrder() {
			return order;
		}

		public String getName() {
			return name;
		}

		public String getEmail() {
			return email;
		}

		public int getStatus() {
			return status;
		}

		public long getSize() {
			return size;
		}

		public String getParams() {
			return params;
		}

		@Override
		protected void loadRequest() 
		{
			start = getInt("start");
			step = getInt("step", 20);
			orderby = getInt("orderby");
			order = getInt("order", 1);
			name = getUnicode("name", SimpleConfig.getConfig("encoding"));
			email = get("email");
			status = getInt("status", -1);

			size = UserService.countByCondition(SessionConstants.USER_ID_ANONYMOUS, name, email);

			List<String[]> nv_pairs = new ArrayList<String[]>();
			nv_pairs.add(new String[]{"step", step+""});
			nv_pairs.add(new String[]{"orderby", orderby+""});
			nv_pairs.add(new String[]{"order", order+""});
			if (name.length() > 0)
				nv_pairs.add(new String[]{"name", name});
			if (email.length() > 0)
				nv_pairs.add(new String[]{"email", email});
			if (status > -1)
				nv_pairs.add(new String[]{"status", status+""});
			params = StringUtils.appendParametersToURL("", nv_pairs, SimpleConfig.getConfig("encoding"));
		}

		@Override
		protected boolean validate() 
		{
			// TODO Auto-generated method stub
			return false;
		}
	}

}
